from django.db import models
from django.utils import timezone
from django.contrib.auth.models import AbstractUser


# 用户
class User(AbstractUser):
    full_name = models.CharField(max_length=100, verbose_name='真实姓名')
    employee_id = models.CharField(max_length=50, verbose_name='工号')
    position = models.CharField(max_length=100, verbose_name='职位')
    group = models.ForeignKey('UserGroup', on_delete=models.SET_NULL, null=True, verbose_name='用户组')

    def __str__(self):
        return f"{self.username}（{self.full_name}）"


# 预测记录
class PredictionRecord(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户', null=True, blank=True)
    selected_epochs = models.JSONField(verbose_name='选择的Epoch')
    model_names = models.JSONField(
        verbose_name='模型名称映射',
        default=dict
    )
    created_at = models.DateTimeField(default=timezone.now, verbose_name='创建时间')

    class Meta:
        verbose_name = '预测记录'
        verbose_name_plural = '预测记录'

    def __str__(self):
        return f"{self.user.username}@{self.created_at}"


# 用户组
class UserGroup(models.Model):
    id = models.AutoField(primary_key=True)
    description = models.CharField(max_length=255)

    def __str__(self):
        return f"Group {self.id}: {self.description}"


# 项目
class Project(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=200)
    created_at = models.DateTimeField(auto_now_add=True)
    created_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name='created_projects')
    total_workload = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)  # 总工作量
    subproject_count = models.IntegerField(default=0)  # 子项目数量
    completed_subproject = models.IntegerField(default=0)  # 当前已完成子项目数量

    def __str__(self):
        return self.name



# 用户-项目关系表（多对多中间模型）
class UserProject(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    project = models.ForeignKey(Project, on_delete=models.CASCADE)

    class Meta:
        unique_together = ('user', 'project')


# 子项目
class SubProject(models.Model):
    id = models.AutoField(primary_key=True)
    project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='subprojects')
    progress = models.DecimalField(max_digits=5, decimal_places=2, default=0.0)  # 实际已完成工作量
    content = models.TextField()
    is_completed = models.BooleanField(default=False)  # 0 表示未完成，1 表示已完成

    def __str__(self):
        return f"SubProject {self.id}"


# 用户-子项目关系表（多对多中间模型）
class UserSubProject(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    subproject = models.ForeignKey('SubProject', on_delete=models.CASCADE)

    class Meta:
        unique_together = ('user', 'subproject')


# 模型表
class ModelInfo(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=200)
    created_at = models.DateTimeField(auto_now_add=True)
    project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='models')

    def __str__(self):
        return self.name


# 预测结果表
class PredictionResult(models.Model):
    subproject = models.OneToOneField(SubProject, on_delete=models.CASCADE, related_name='prediction')
    predicted_effort = models.DecimalField(max_digits=10, decimal_places=2)

    def __str__(self):
        return f"Prediction for SubProject {self.subproject.id}: {self.predicted_effort}"
